package com.bdqn.zhang.dao.impl;

import com.bdqn.zhang.dao.VivodingdanDao;
import com.bdqn.zhang.dao.impl.dbutil.DButil;
import com.bdqn.zhang.pojo.Memberinfo;
import com.bdqn.zhang.pojo.Vivodingdan;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class VivodingdanDaoimpl extends DButil implements VivodingdanDao {
    Connection conn = null;
    PreparedStatement ment = null;
    ResultSet resultSet = null;

    @Override
    public List<Vivodingdan> getAll(int from, int pageSixe) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Vivodingdan> list = new ArrayList();
        String sql = "select * from Vivodingdan limit ?,?";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            ment.setObject(1, from);
            ment.setObject(2, pageSixe);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Vivodingdan vivodingdan = new Vivodingdan();
                    vivodingdan.setId(resultSet.getInt(1));
                    vivodingdan.setDingdanbh(resultSet.getString(2));
                    vivodingdan.setAdd_time(resultSet.getString(3));
                    vivodingdan.setName(resultSet.getString(4));
                    vivodingdan.setImg(resultSet.getString(5));
                    vivodingdan.setSpname(resultSet.getString(6));
                    vivodingdan.setJg(resultSet.getInt(7));
                    vivodingdan.setShuliang(resultSet.getInt(8));
                    vivodingdan.setShouhou(resultSet.getString(9));
                    vivodingdan.setZt(resultSet.getInt(10));
                    vivodingdan.setShoukuan(resultSet.getInt(11));
                    list.add(vivodingdan);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int getCount() {
        conn = getConnerction();
        String sql = "select count(1) from Vivodingdan";
        try {
            ment = conn.prepareStatement(sql);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }
}
